package com.chinacoal.hr.levelimpl.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.entity.HolidayLaws;
import com.chinacoal.hr.levelimpl.dao.HolidayLawsDao;
import com.chinacoal.hr.levelimpl.service.HolidayLawsService;
import com.chinacoal.hr.levelimpl.utils.DateUtil;
import com.chinacoal.hr.levelimpl.utils.StaticDicts;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: YunTao.Li
 * @create: 2019-12-18 22:49
 * @description: 假期排班service实现
 **/
@Service
public class HolidayLawsServiceImpl implements HolidayLawsService {

    @Autowired
    private HolidayLawsDao holidayLawsDao;

    /**
     * 查询假期排班信息列表
     *
     * @param page  :
     * @param limit :
     * @param year  :
     * @param hdate :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.HolidayLaws>
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    @Override
    public IPage<HolidayLaws> getHolidayLawsList(long page, long limit, String year, String hdate) throws Throwable {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("year", year);
        condition.put("hdate", hdate);

        IPage<HolidayLaws> holidayLawsIPage = null;
        try {
            holidayLawsIPage = holidayLawsDao.getHolidayLawList(condition);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }

        return holidayLawsIPage;
    }

    /**
     * 新增假期排班信息
     *
     * @param record :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    @Override
    public Map<String, String> insertHolidayLaw(HolidayLaws record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");
        try {
            // step1 : 根据开始时间与结束时间算出天数
            Date startDate = record.getStartDate();
            Date endDate = record.getEndDate();
            int days = DateUtil.getNatureDayMargin(startDate, endDate);

            // step2 : 循环遍历天数
            String holidayBatchid = UUID.randomUUID().toString();
            for (int i = 1; i <= days; i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + i - 1);

                // step2.1 : 首先计算日期，
                Date currentDate = calendar.getTime(); //当前天
                int year = calendar.get(Calendar.YEAR);

                HolidayLaws currentHoliday = new HolidayLaws();
                BeanUtils.copyProperties(record, currentHoliday);
                currentHoliday.setBatchId(holidayBatchid);
                currentHoliday.setHDate(currentDate);
                currentHoliday.setDays(days);
                currentHoliday.setYear(String.valueOf(year));
                holidayLawsDao.insertHolidayLaw(currentHoliday);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
        result.put("code", "1");
        return result;
    }

    /**
     * 更新假期排班信息
     *
     * @param record :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    @Override
    public Map<String, String> updateHolidayLaw(HolidayLaws record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");
        try {
            holidayLawsDao.updateHolidayLaw(record);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
        result.put("code", "1");
        return result;
    }

    /**
     * 删除假期排班信息
     *
     * @param holidayId :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    @Override
    public Map<String, String> deleteHolidayLaw(String holidayId) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");
        try {
            holidayLawsDao.deleteHolidayLawById(holidayId);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
        result.put("code", "1");
        return result;
    }

    /**
     * 返回传入的日期是否法定假日或者正常上班
     * 比如2020-10-10日是周六，但是数据返回2，则这一天正常上班
     * 比如2020-10-07日是周四，但是数据返回1，则这一天为法定假日
     *
     * @param date :
     * @return : java.lang.String 0数据不存在，1法定假日，2正常上班
     * @author : YunTao.Li
     * @date : 2020/7/23 2020/7/23
     */
    @Override
    public String getHolidayHType(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String hDateStr = format.format(date);
        Map condition = new HashMap();
        condition.put("hDate", hDateStr);

        String hType = StaticDicts.HTYPE_NODATA;
        HolidayLaws holidayLaws = this.holidayLawsDao.getHolydayLow(condition);

        if (null != holidayLaws) {
            hType = holidayLaws.getHType();
        }
        return hType;
    }
}
